/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package qs.crm.modules.client.orders.service.impl;

import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import qs.crm.modules.client.cstLost.repository.CstLostRepository;
import qs.crm.modules.client.orders.domain.Orders;
import qs.crm.modules.client.orders.repository.OrdersRepository;
import qs.crm.modules.client.orders.service.OrdersService;
import qs.crm.modules.client.orders.service.dto.OrdersDto;
import qs.crm.modules.client.orders.service.dto.OrdersQueryCriteria;
import qs.crm.modules.client.orders.service.mapstruct.OrdersMapper;
import qs.crm.utils.FileUtil;
import qs.crm.utils.PageUtil;
import qs.crm.utils.QueryHelp;
import qs.crm.utils.ValidationUtil;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author tong
* @date 2023-06-01
**/
@Service
@RequiredArgsConstructor
public class OrdersServiceImpl implements OrdersService {

    private final OrdersRepository ordersRepository;
    private final OrdersMapper ordersMapper;
    private final CstLostRepository cstLostRepository;

    @Override
    public Map<String,Object> queryAll(OrdersQueryCriteria criteria, Pageable pageable){
        Page<Orders> page = ordersRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ordersMapper::toDto));
    }

    @Override
    public List<OrdersDto> queryAll(OrdersQueryCriteria criteria){
        return ordersMapper.toDto(ordersRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public OrdersDto findById(Long odrId) {
        Orders orders = ordersRepository.findById(odrId).orElseGet(Orders::new);
        ValidationUtil.isNull(orders.getOdrId(),"Orders","odrId",odrId);
        return ordersMapper.toDto(orders);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public OrdersDto create(Orders resources) {
        return ordersMapper.toDto(ordersRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Orders resources) {
        Orders orders = ordersRepository.findById(resources.getOdrId()).orElseGet(Orders::new);
        ValidationUtil.isNull( orders.getOdrId(),"Orders","id",resources.getOdrId());
        orders.copy(resources);
        ordersRepository.save(orders);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long odrId : ids) {
            ordersRepository.deleteById(odrId);
        }
    }

    @Override
    public void download(List<OrdersDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (OrdersDto orders : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("客户名称", orders.getOdrCustomer());
            map.put("下单日期", orders.getOdrDate());
            map.put("客户地址", orders.getOdrAddr());
            map.put("订单状态	(可选值为5-6)", orders.getOdrStatus());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void nameUpdat(String lstCustName) {
        cstLostRepository.nameUpdate(lstCustName);
    }
}